String.prototype.format = function(args) {
    if (arguments.length>0) {
        var result = this;
        if (arguments.length == 1 && typeof (args) == "object") {
            for (var key in args) {
                var reg=new RegExp ("({"+key+"})","g");
                result = result.replace(reg, args[key]);
            }
        }
        else {
            for (var i = 0; i < arguments.length; i++) {
                if(arguments[i]==undefined)
                {
                    return "";
                }
                else
                {
                    var reg=new RegExp ("({["+i+"]})","g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
        return result;
    }
    else {
        return this;
    }
}

function logout(text,color) {
    var myDate = new Date();
    var outtext = myDate.getHours() + ':' + myDate.getMinutes() + ':' + myDate.getSeconds() + '=>' + text;
    console.log("%c{outtext}".format({"outtext":outtext}),"color:{color}".format({"color":color}));
}

var count=0;

function listen() {
    var url="http://api.live.bilibili.com/activity/v1/NewSpring/redBagPool"
    $.ajax({
        url:url,
        success:function (res) {
            if(res.code==0){
                var myDate = new Date();
                var red_bag_num=res.data.red_bag_num;
                for(index in res.data.pool_list)
                {
                    var pool_name=res.data.pool_list[index].award_name;
                    if (pool_name.match("舰长体验券")) 
                    {
                        var stock_num=res.data.pool_list[index].stock_num;
                        if(stock_num!=0){
                            var awardid=res.data.pool_list[index].award_id;
                            $.ajax({
                                type: "post",
                                url: "http://api.live.bilibili.com/activity/v1/NewSpring/redBagExchange",
                                data: {
                                    award_id: awardid,
                                    exchange_num: 1
                                },
                                success:function (res) {
                                    logout(res.message,"green");
                                },
                                xhrFields : {
                                    withCredentials : true
                                }
                            })
                        }
                        else{
                            count++;
                            if(count>11){
                                logout("当前{0}无库存".format(pool_name),"gray");
                                count=0;
                            }
                        }
                    }
                }
              
            }
        },
        xhrFields : {
            withCredentials : true
        }

    })
    
}
if (typeof (is_start) == 'undefined') {
    is_start = 1;
    alert('启动成功，会自动更换红灯笼，F12 Console控制台可以查看实时状况');
    setInterval(function () { listen() }, 900)
}
else { alert('当前页面已经开启脚本，请勿重复开启'); }